Arrays Class in Java
Code:
- Equals and toString Methods: https://ide.geeksforgeeks.org/KZdDEHxI17
In this video we'll talk about Fill in Java
Codes:
In this video we'll talk about Binary search in Java
Codes:
Equals Method in Arrays Class
Codes:
Arrays Mismatch
Codes:
Compare Function In Arrays Class
Codes:
In this video we'll talk about Arrays.Aslist function
Codes:
Arrays To String in Java
Codes:
true
[10, 20, 30]
Syntax:
// Makes all elements of a[] equal to "val"
public static void fill(int[] a, int val)
// Makes elements from from_Index (inclusive) to to_Index
// (exclusive) equal to "val"
public static void fill(int[] a, int from_Index, int to_Index, int val)
This method doesn't return any value.
Exceptions it Throws:
IllegalArgumentException - if from_Index > to_Index
ArrayIndexOutOfBoundsException - if from_Index a.length
Array completely filled with 10
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[2, 10, 10, 10, 10, 2, 2, 2, 2]
[[10, 10, 10, 10], [10, 10, 10, 10], [10, 10, 10, 10]]2) Fill 3D Array
[[[-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1]], [[-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1]], [[-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1]]]
public static int binarySearch(data_type arr, data_type key )where data_type can be any of the primitive data types: byte, char, double, int, float, short, long and Object as well.
public static int binarySearch( data_type[] arr, int fromIndex, int toIndex, data_type key )where data_type can be any of the primitive data types: byte, char, double, int, float, short, long and Object as well.
1
2
-3
// A sample Java program to implement
// Arrays.binarySearch() for non-primitive types
import java.util.Arrays;
// A user-defined Point class implementing
// Comparable interface
class Point implements Comparable<Point>
{
int x, y;
// Costructor intialising x & y
Point(int x, int y)
{
this.x = x;
this.y = y;
}
// compareTo() function defining the
// nature of sorting i.e., according to
// x-coordinate
public int compareTo(Point P)
{
// Comparing two objects by
// Subtracting the passed object
// from current object
// This compares the passed element
// with the middle element and decides
// whether to go left or right of the array
return this.x - P.x;
}
}
// Main class
class Test
{
public static void main(String[] args)
{
// Array of 3 objects
Point arr[] = {
new Point(10, 20),
new Point(20, 15),
new Point(25, 5),
new Point(40, 100)};
Point p = new Point(25, 5);
System.out.println(Arrays.binarySearch(arr, p));
}
} 2
// A sample Java program to implement
// Arrays.binarySearch() for non-primitive types
import java.util.*;
// Point class which does not implement
// Comparable interface. Thus the objects
// of this class are not comparable.
class Point
{
int x, y;
// Costructor intialising x & y
Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
// This class implements
// Comparator interface to compare
class MyCmp implements Comparator<Point>
{
// Sorts the Point objects according
// to x-coordinates in natural order
public int compare(Point p1, Point p2)
{
// Considered the natural order
return p1.x - p2.x;
}
}
// Main class
class Test
{
public static void main(String[] args)
{
// Array of 3 objects
Point arr[] = {
new Point(10, 20),
new Point(20, 15),
new Point(25, 5),
new Point(40, 100)};
Point p = new Point(25, 5);
// An extra parameter is passed with the
// object of MyCmp class
System.out.println(Arrays.binarySearch(
arr, p, new MyCmp()));
}
} 2
public static boolean equals(a[], b[])Parameters:
public static boolean equals(a[], aStart, aEnd, b[], bStart, bEnd)Parameters:
Yes
No
Yes
No
Yes
Yes
-1
2
-1
1
a is smaller
a is greater
a is greater
a is smaller
a is smaller
Same
Yes
public static List asList(T... a)
[GfG, IDE, Courses]
[Practice, IDE, Courses]
[Practice, Premium, Courses]
[Courses, IDE, GfG]
[GfG, Courses, IDE]
[I@232204a1
[10, 20, 30]
[(10, 20), (5, 30)]